<template>
  <div >
    <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
      <div class="widget am-cf">
        <div class="widget-head am-cf">
          <div class="widget-title  am-cf">上课日期调整</div>
        </div>
        <div class="widget-body  am-fr">

          <div class="am-u-sm-12 am-form ">

            <div class="am-u-sm-12 am-u-md-12 am-u-lg-3">
              <div class="am-form-group">
                <select2  v-model="query.areaTeamId" :options="areaTeams">
                  <option value="" disabled>区域</option>
                </select2>
              </div>
            </div>
            <el-select size="small" class="am-u-sm-12 am-u-md-12 am-u-lg-3 am-form-group" multiple
                       :disabled="busTeams.length === 0" v-model="query.busTeamIds" placeholder="请选择业务组">
              <el-option
                v-for="item in busTeams"
                :key="item.busTeamId"
                :label="item.name"
                :value="item.busTeamId">
              </el-option>
            </el-select>
            <div class="am-u-sm-12 am-u-md-12 am-u-lg-3 am-form-group">
              <el-date-picker
                v-model="year"
                type="year"
                :clearable="false"
                :editable="false"
                placeholder="请选择年份">
              </el-date-picker>
            </div>
            <el-select :disabled="periods.length === 0" size="small" class="am-u-sm-12 am-u-md-12 am-u-lg-3 am-form-group" multiple
                       v-model="query.periodIds" placeholder="请选择期数">
              <el-option
                v-for="item in periods"
                :key="item.periodId"
                :label="item.periodName"
                :value="item.periodId">
              </el-option>
            </el-select>

            <el-select size="small" class="am-u-sm-12 am-u-md-12 am-u-lg-3 am-form-group" multiple
                       v-model="query.gradeIds" placeholder="请选择年级">
              <el-option
                v-for="item in grades"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
            <el-select size="small" class="am-u-sm-12 am-u-md-12 am-u-lg-3 am-form-group"
                       v-model="query.subjectId" placeholder="请选择科目">
              <el-option
                v-for="item in subjects"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>

            <el-select size="small" class="am-u-sm-12 am-u-md-12 am-u-lg-3 am-form-group"
                       v-model="query.segmentNo" placeholder="请选择段次">
              <el-option
                v-for="item in segmentNos"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>


            <div class="am-u-sm-12 am-u-md-12 am-u-lg-3">
              <div class="am-form-group">
                <input type="text" class="am-form-field" placeholder="班级编号"  v-model="query.classNo">
              </div>
            </div>

            <div class="am-u-sm-12 am-u-md-12 am-u-lg-3">
              <div class="am-form-group">
                <input type="text" name="className" v-model="query.className" placeholder="请输入班级名称"/>
              </div>
            </div>

            <div class="am-u-sm-12 am-u-md-12 am-u-lg-3">
              <div class="am-form-group">
                <input type="text" name="teacherName" v-model="query.teacherName" placeholder="任课教师姓名"/>
              </div>
            </div>

            <div class="am-u-sm-12 am-u-md-12 am-u-lg-3">
              <div class="am-form-group">
                <el-date-picker
                  v-model="query.time"
                  type="daterange"
                  placeholder="上课日期">
                </el-date-picker>
              </div>
            </div>
            <div class="am-u-sm-12 am-u-md-12 am-u-lg-3">
              <div class="am-form-group">
                <select2  v-model="query.timeSegment">
                  <option value="">上课时间</option>
                  <option value="00:00-08:00">00:00-08:00</option>
                  <option value="08:00-10:00">08:00-10:00</option>
                  <option value="10:00-12:00">10:00-12:00</option>
                  <option value="12:00-14:00">12:00-14:00</option>
                  <option value="14:00-16:00">14:00-16:00</option>
                  <option value="16:00-18:00">16:00-18:00</option>
                  <option value="18:00-20:00">18:00-20:00</option>
                  <option value="20:00-22:00">20:00-22:00</option>
                  <option value="22:00-24:00">22:00-24:00</option>
                </select2>
              </div>
            </div>
            <div class="am-u-sm-12 am-u-md-12 am-u-lg-3">
              <div class="am-form-group">
                <input type="text" class="am-form-field" placeholder="讲次"  v-model="query.lectureNo">
              </div>
            </div>
            <div class="am-u-sm-12 am-u-md-12 am-u-lg-3 am-u-end">
              <div class="am-form-group">
                <button type="button" class="am-btn am-btn-default am-btn-success"
                        @click="search" ><span class="am-icon-search"></span>&nbsp;查询
                </button>
              </div>
            </div>

            <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
              <div class="am-form-group am-btn-group-xs">
                <button type="button" class="am-btn am-btn-default am-btn-success" @click="handleAdjust">批量调整日期</button>
                <button type="button" class="am-btn am-btn-default am-btn-success" @click="handleAdjustTime">批量调整时间</button>
              </div>
            </div>
          </div>

          <div class="am-u-sm-12">
            <el-table
              :data="tableData"
              border
              stripe
              style="min-width: 100%"
              @selection-change="handleSelectionChange">
              <el-table-column
                type="selection"
                width="55">
              </el-table-column>
              <el-table-column
                prop="lectureNo"
                label="讲次"
                min-width="100">
              </el-table-column>
              <el-table-column
                prop="classNo"
                label="班级编号"
                min-width="120">
              </el-table-column>
              <el-table-column
                prop="className"
                label="班级名称"
                min-width="160">
              </el-table-column>
              <el-table-column
                prop="gradeName"
                label="年级"
                min-width="120">
              </el-table-column>
              <el-table-column
                prop="subjectName"
                label="科目"
                min-width="120">
              </el-table-column>
              <el-table-column
                prop="busTeamName"
                label="业务组"
                min-width="120">
              </el-table-column>
              <el-table-column
                prop="periodName"
                label="期数"
                min-width="120">
              </el-table-column>
              <el-table-column
                prop="segmentNo"
                label="段次"
                min-width="120">
              </el-table-column>
              <el-table-column
                prop="name"
                label="上课日期"
                min-width="120">
                <template scope="scope">
                  {{scope.row.classDate | formatDate }}
                </template>
              </el-table-column>
              <el-table-column
                prop="name"
                label="上课时间"
                min-width="120">
                <template scope="scope">
                  {{scope.row.startTime + '-' + scope.row.endTime}}
                </template>
              </el-table-column>
              <el-table-column
                prop="teacherName"
                label="任课老师"
                min-width="120">
              </el-table-column>
            </el-table>
          </div>
          <window ref="win" title="将所选讲次的上课日期批量调整为：">
            <template>
              <div class="add-check">
                <div class="am-form-group am-text-center">
                  <div style="width: 80%; margin: 20px auto">
                    <el-date-picker
                      v-model="form.classDate"
                      type="date"
                      placeholder="上课日期">
                    </el-date-picker>
                    <el-checkbox v-model="form.updateAfterLecture">后续讲次顺延</el-checkbox>
                  </div>
                </div>
                <div class="am-form-group am-text-center">
                  <el-button type="primary" @click="batchAdjustLectures">确定</el-button>
                </div>
              </div>
            </template>
          </window>
          <window ref="winTime" title="将所选讲次的上课时间批量调整为：">
            <template>
              <div class="add-check">
                <div class="am-form-group am-text-center">
                  <div style="width: 80%; margin: 20px auto">
                    <time-picker v-model="times"></time-picker>
                  </div>
                </div>
                <div class="am-form-group am-text-center">
                  <el-button type="primary" @click="batchAdjustLectureTimes">确定</el-button>
                </div>
              </div>
            </template>
          </window>
          <div class="am-u-lg-12 am-cf">

            <div class="am-fr">
              <pagination @pageSizeChange="pageSize = arguments[0]" isShowPageSize v-bind:total="total" v-bind:pageNo="pageNo" v-bind:pageSize="pageSize" @paging="loadTableData" />
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import io from '../../lib/io'
  import moment from 'moment'
  import Pagination from '../base/Pagination'
  import ElButton from "../../../node_modules/element-ui/packages/button/src/button.vue";

  export default{
    data:function(){
      return {
        form:{
          classDate: moment().format('YYYY-MM-DD'),
          updateAfterLecture:false,

        },
        times:"08:00-10:00",
        classLectureIds:[],

        tableData: [],
        total: 0,
        pageSize: 10,
        pageNo: 1,
        query: {
          areaTeamId : window.config.areaTeams[0] && window.config.areaTeams[0].areaTeamId || '' ,
          busTeamIds : [],
          periodIds : [],
          isAttendance : '',
          teacherName: '',
          classNo: '',
          className: '',
          startDate: '',
          endDate: '',
          gradeIds: [],
          subjectId: '',
          segmentNo: '',
          time: [],
          timeSegment:'',
          lectureNo:"",
        },
        year: moment().format('YYYY'),
        segmentNos: [
          {label: '请选择段次',value: ''},
          {label: '段次1',value: 1},
          {label: '段次2',value: 2},
          {label: '段次3',value: 3},
          {label: '段次4',value: 4},
          {label: '段次5',value: 5},
          {label: '段次6',value: 6},
          {label: '段次7',value: 7},
          {label: '段次8',value: 8},
        ],
        periods:[],
      }
    },
    components: {
      ElButton,
      Pagination
    },
    mounted:function(){
      $(window).smoothScroll()
    },
    created:function(){
//      this.loadTableData(this.pageNo);
      this.loadPeriodByYear()
    },
    watch: {
      year (newVal, oldVal) {
        this.loadPeriodByYear()
        this.query.periodIds = []
      },
      'query.areaTeamId':function(){
          this.query.periodIds = []
          this.loadPeriodByYear()
      }
    },
    computed: {
      areaTeams: function () {
        var options = ( this.$root.config.areaTeams || [] )
          .map(function (item) {
            return {value: item.areaTeamId, text: item.name}
          })
        return options
      },
      busTeams: function () {
        return ( ( this.query.areaTeamId  ) ? ( this.$root.config.groupBusTeams[this.query.areaTeamId] || [] ) : [] )
      },
      grades () {
        return this.$root.config.grades.map(function(item){
          return {value: item.gradeId, label: item.gradeName}
        })
      },
      subjects () {
        return [{value: '', label: '全部科目'}].concat(
          this.$root.config.subjects.map(function(item){
            return {value: item.subjectId, label: item.subjectName}
          })
        )
      }
    },
    methods:{
      handleAreaTeamIdChange() {
        this.loadPeriodByYear()
        this.formData.busTeamIds = []
        this.formData.periodIds = []
      },
      handleAdjust () {
        if (!this.classLectureIds || !this.classLectureIds.length) {
          this.$alert('请至少选择一条记录')
          return
        }
        this.$refs.win.show({width:600, height:600})
      },
      handleAdjustTime(){
        if (!this.classLectureIds || !this.classLectureIds.length) {
          this.$alert('请至少选择一条记录')
          return
        }
        this.$refs.winTime.show({width:600, height:600})
      },
      batchAdjustLectureTimes(){
        var _this = this
        if (!this.times) {
          this.$alert('请选择上课时间')
          return
        }
        io.post(io.batchAdjustLectureTimes,{
          classLectureIds: this.classLectureIds.join(','),
          time:this.times
        },(ret)=>{
          if(ret.success){
            this.$toast('调整成功,如发现未更改请重新查询')
            _this.$refs.winTime.close()
            _this.$showLoading()
            setTimeout(function(){
              //重新编辑公式
              _this.loadTableData()
              },800);
            _this.$hiddenLoading()
          }else{
            this.$alert(ret.desc)
          }
        })
      },
      batchAdjustLectures () {
         var _this = this
        if (!this.form.classDate) {
          this.$alert('请选择上课日期')
          return
        }
        io.post(io.batchAdjustLectures,{
          classLectureIds: this.classLectureIds.join(','),
          classDate: moment(this.form.classDate).format('YYYY-MM-DD'),
          updateAfterLecture:this.form.updateAfterLecture,
        },(ret)=>{
          if(ret.success){
            this.$toast('调整成功,如发现未更改请重新查询')
            _this.$refs.win.close()
            _this.$showLoading()
            setTimeout(function(){
              //重新编辑公式
              _this.loadTableData()
              },800);
            _this.$hiddenLoading()
          }else{
            this.$alert(ret.desc)
          }
        })
      },
      handleSelectionChange (value) {
        this.classLectureIds = []
        value.map(val => {
          this.classLectureIds.push(val.classLectureId)
        })
      },
      loadPeriodByYear: function () {
        var _this = this
        io.post(io.periodByYearAndAreaTeamId,{
          areaTeamId: this.query.areaTeamId,
          year: this.year ? moment(this.year).format('YYYY'): '',

        },function(ret){
          if(ret.success){
            _this.periods = ret.data
            if (_this.periods.length === 0) {
              _this.$alert('当前没有设置期数')
            }
          }else{
            _this.$alert(ret.desc)
          }
        })
      },
      search:function(){
        this.loadTableData(1)
      },
      loadTableData:function(pageNo){
        var _this = this
        _this.pageNo = pageNo || _this.pageNo || 1
        let data = Object.assign({},this.query, {
          pageNo:this.pageNo,
          pageSize:this.pageSize,
          busTeamIds:this.query.busTeamIds.join(','),
          gradeIds:this.query.gradeIds.join(','),
          periodIds:this.query.periodIds.join(','),
          startDate: this.query.time&&this.query.time[0] ? moment(this.query.time[0]).format('YYYY-MM-DD'): '',
          endDate: this.query.time&&this.query.time[0] ? moment(this.query.time[1]).format('YYYY-MM-DD'): '',
          greateOrEqualStartTime:this.query.timeSegment?this.query.timeSegment.split('-')[0]:'',
          lessOrEqualEndTime:this.query.timeSegment?this.query.timeSegment.split('-')[1]:'',
        })
        io.post(io.findClassLectureDetailPage,data,function(ret){
          if(ret.success){
            _this.total = ret.data.total
            _this.tableData = ret.data.list
          }else{
            _this.$alert(ret.desc)
          }
        })
      },
    }
  }
</script>
<style lang="less">
  .am-form input[type=text][readonly] {
    cursor: text;
    background-color: transparent;
  }
  .add-check{
     .el-date-editor.el-input{
       width:60%;
     }
     .el-checkbox{
       margin-left:10px;
       color:red;
     }
  }

</style>
<style lang="less" scoped>

</style>

